<?php
include_once("KillCommonModel.class.php");
include_once("Order.class.php");
include_once("Goods.class.php");
class MysqlKill extends KillCommonModel{

    public $db="goods";

    public $mysqlIp="localhost";

    public $userDb="root";

    public $userPwd="root";

    private $conn=null;

    public function setConfig($db,$mysqlIp,$userDb,$userPwd){
        $this->db=$db;
        $this->mysqlIp=$mysqlIp;
        $this->userDb=$userDb;
        $this->userPwd=$userPwd;
    }

    /**
     * 抢购入口
     * @param $id
     * @param $userId
     */
    public  function RunPurchase($id,$userId){
        $this->goodsIdBy=$id;
        $this->userId;
        $this->Run();
    }

    /*
     * 运行抢购所需要的类及方法
     */
    private function Run(){
        if(!$this->checkClassExists()){
            return false;
        }
        $this->getCoon();
        $this->Manage();
    }

    /*
     * 链接数据
     */
    private function getCoon(){
        $conn=$this->conn=mysql_connect($this->mysqlIp,$this->userDb,$this->userPwd);
        mysql_select_db($this->db,$conn);
        mysql_query("set names utf8");
    }

    /*
     * 购买商品业务 下单
     */
    private function Manage(){
        $goodsObj=$this->goodsObj;
        $where=array($this->goodsId=>$this->goodsIdBy);
        $goodsInfo=$goodsObj->getRowGoodsInfo($where);
        if($goodsInfo==null || !$goodsInfo){
            $this->error="商品不存在";
            return false;
        }
        //此时这条记录被锁住,其它事务必须等待此次事务提交后才能执行
        mysql_query("BEGIN");	//开始事务
        $sql="select  '.$this->counts.' from '.$this->goodsTable.' where '.$this->goodsId.'='.$this->goodsIdBy.' FOR UPDATE";
        $rs=mysql_query($sql,$this->conn);
        $row=mysql_fetch_assoc($rs);
        if($row[$this->counts]<=0){
            mysql_query("ROLLBACK");
            mysql_query("COMMIT");//事务提交即解锁
            $this->error="商品库存不足";
            return false;
        }
        $resul=$this->orderInfo=$this->oderObj->createOrder($goodsInfo,$this->userId);
        if(!$resul){
            mysql_query("COMMIT");//事务提交即解锁
            $this->error="购买超时，请重试 ";
            return false;
        }
        $sql="update ih_store set {$this->counts}={$this->counts}-1 where {$this->goodsId}='".$this->goodsIdBy."'";
        $store_rs=mysql_query($sql,$this->conn);
        if(!mysql_affected_rows()){
            mysql_query("ROLLBACK");
            mysql_query("COMMIT");
            $this->error="购买超时，请重试 ";
            return false;
        }
        mysql_query("COMMIT");
        return true;
    }

    /*
     * 检查初始化配置是否存在
     * @return bool
     */
    private function checkClassExists(){
        if(!class_exists($this->goodsObj)){
            $this->error="商品表类不存在";
            return false;
        }
        $this->goodsObj=$goodsObj=new $this->goodsObj();
        if(!method_exists($goodsObj,"getRowGoodsInfo")){
            $this->error="商品表类中getRowGoodsInfo方法不存在";
            return false;
        }
        if(!method_exists($goodsObj,"updateCount")){
            $this->error="商品表类中updateCount方法不存在";
            return false;
        }
        if(!class_exists($this->oderObj)){
            $this->error="订单表类不存在";
            return false;
        }
        $this->oderObj=$orderObj=new $this->oderObj();
        if(!method_exists($orderObj,"createOrder")){
            $this->error="订单表类中createOrder方法不存在";
            return false;
        }
        return true;
    }
}